{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding=utf8\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "import random\n",
    "import numpy as np\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pre</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.0</td>\n",
       "      <td>[0.7311495, 0.2688505]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>[0.00013767493, 0.99986232507]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>[3.0467527e-05, 0.999969532473]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>[6.579378e-05, 0.99993420622]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.0</td>\n",
       "      <td>[0.99987155, 0.00012844999999994666]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.0</td>\n",
       "      <td>[0.9999719, 2.8099999999975367e-05]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.0</td>\n",
       "      <td>[0.9998221, 0.0001778999999999531]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.0</td>\n",
       "      <td>[0.9985736, 0.0014264000000000499]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1.0</td>\n",
       "      <td>[4.2013155e-05, 0.999957986845]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1.0</td>\n",
       "      <td>[4.3707863e-05, 0.999956292137]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   pre                                 score\n",
       "0  0.0                [0.7311495, 0.2688505]\n",
       "1  1.0        [0.00013767493, 0.99986232507]\n",
       "2  1.0       [3.0467527e-05, 0.999969532473]\n",
       "3  1.0         [6.579378e-05, 0.99993420622]\n",
       "4  0.0  [0.99987155, 0.00012844999999994666]\n",
       "5  0.0   [0.9999719, 2.8099999999975367e-05]\n",
       "6  0.0    [0.9998221, 0.0001778999999999531]\n",
       "7  0.0    [0.9985736, 0.0014264000000000499]\n",
       "8  1.0       [4.2013155e-05, 0.999957986845]\n",
       "9  1.0       [4.3707863e-05, 0.999956292137]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "save_test = pd.read_csv('data/save_test.tsv', sep = '\\t', names = ['pre', 'score'])\n",
    "def fun(x):\n",
    "    \"\"\"\n",
    "    \"\"\"\n",
    "    data = x[1:-1].split(' ')\n",
    "    a = float(data[0])\n",
    "    return [a, 1. - a]\n",
    "save_test.score = save_test.score.apply(fun)\n",
    "save_test.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "614\n",
      "0.1573552024602768\n"
     ]
    }
   ],
   "source": [
    "cnt = 0\n",
    "confidence_gap = 0.999\n",
    "unconfidence_idxs = []\n",
    "len_test_set = len(list(save_test['score']))\n",
    "for i in range(len_test_set):\n",
    "    max_score = max(save_test.iloc[i]['score'])\n",
    "    if float(max_score) < confidence_gap:\n",
    "        unconfidence_idxs.append(i)\n",
    "        cnt += 1\n",
    "print (cnt)\n",
    "print ((cnt + 0.) / len_test_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_gbm = pd.read_csv('submit_user_info.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "for idx in set(save_test.index) - set(unconfidence_idxs):\n",
    "    test_gbm.loc[idx, 'label'] = save_test.loc[idx, 'pre']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_gbm.to_csv('merge_submit.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
